<template>
  <view class="wrap">
    <top-tab tabTitle="交易记录" @backClick="backClick" backColor="#5B2177"></top-tab>
		<!-- 顶部选项卡 -->
		<view class="tabs-box">
			<ms-tabs :list="list" v-model="tabActive" itemColor='#A912E7' lineColor='#A912E7' @input="matabInput"/>
		</view>
		<scroll-view scroll-y="true" refresher-enabled="true" class="sv" :refresher-triggered="triggered" :refresher-threshold="100"
			@refresherrefresh="onRefresh" @refresherabort="onAbort" @scrolltolower="scrollBottom" :style="{height:navHeight+'px'}">
			<view class="main" v-if="takeList.length">
				<view class="deal-list" v-for="(item,index) in takeList" :key="item.id">
					<view class="item-title-time">
						<view class="title">{{item.id}}</view>
						<view class="apply-time">{{item.createTime}}</view>
					</view>
					<view class="take-put-money">
						<view  class="take-money">{{item.goodsName}}</view>
						<view class="money">{{item.goodsPrice}}</view>
					</view>
					<view class="deal-time-status">
						<view  class="item-time">{{item.updateTime}}</view>
						<view class="status" :style="{color: item.color}">{{item.stateName}}</view>
					</view>
				</view>
				<uni-load-more style="padding: 20rpx;"  :status="loadingType" iconType="auto"></uni-load-more>
			</view>
			<no-data v-else></no-data>
		</scroll-view>
  </view>
</template>

<script>
	import topTab from '@/components/top-tabbar.vue'
	import msTabs from '@/components/ms-tabs.vue'
	import { $POST, $GET } from '@/network/api.js'
	import { formatDate } from '@/network/date.js'
	import noData from '@/components/nodata.vue'
  export default {
		components: { topTab, msTabs, noData },
    data() {
      return {
				buyFrom: { // 交易记录参数
					pageNum: 1,
					pageSize: 10,
					orderState: '' , // 订单状态：支付中=0，1=成功，2=取消购买，3=失败（拒绝和处理失败），4=待审核
				},
				videoList: [], // 用于分页长度
				loadingType: 'loading', // 加载更多状态
				navHeight:'', // 主体高度
				triggered: false, // 下拉刷新开关
				takeList: [], // 列表数据
				tabActive: 0,
					list:[
						{ title:'充值记录', value: 1 }, { title:'提现记录', value: 2 },
					],
			}
    },
  onLoad() {
  	this.getBuy()
  },
  onReady(){
  	this.getHight() // 获取高度
  },
  methods: {
		// 关闭下拉刷新
		onAbort() {
			const vm = this
			setTimeout(() => {
				vm.triggered = false;
			}, 100)
		},
		// 下拉刷新
		onRefresh() {
			const vm = this
			vm.triggered = true	
			vm.getBuy() 		
		 },
		// 滚动到底部
		scrollBottom() {
			this.buyFrom.pageNum = this.buyFrom.pageNum +1
			if(this.videoList.length < 10) {
				this.loadingType='nomore'
				return
			}
			this.getBuy() 
		},
		// 获取设备高度
		getHight() {
			const vm = this;
			uni.getSystemInfo({ //调用uni-app接口获取屏幕高度
				success(res) { //成功回调函数
					let windowHeight = res.windowHeight //windoHeight为窗口高度，主要使用的是这个
					let titleH = uni.createSelectorQuery().select(".sv")
					titleH.boundingClientRect(data=>{
						vm.navHeight = windowHeight - data.top //计算高度：元素高度=窗口高度-元素距离顶部的距离（data.top）
					}).exec()
				}
			})
		},
		// 获取购买记录列表
		async getBuy() {
			let res = ''
				if (this.tabActive == 0) {
					res = await $GET("/boke-pay/recharge/queryList",this.buyFrom)
					this.onAbort()
					this.videoList = JSON.parse(JSON.stringify(res.data))
					this.videoList.forEach(item => {
						item.createTime = formatDate(item.createTime)
						item.updateTime = formatDate(item.updateTime || 0)
						if(item.state === 1) {
							item.stateName = "使用中"
							item.color = "#10B86B"
						}
						if(item.state === 2) {
							item.stateName = "已到期"
							item.color = "#F11616 "
						}
						if(item.state === 3) {
							item.stateName = "未使用"
							item.color ="#4599F6"
						}
					})
					 this.takeList = [...this.takeList,... this.videoList]
				}else{
					res = await $GET("/boke-pay/withdraw/queryList",this.buyFrom)
					this.onAbort()
					this.videoList = JSON.parse(JSON.stringify(res.data))
					this.videoList.forEach(item => {
						item.createTime = formatDate(item.createTime)
						item.updateTime = formatDate(item.updateTime || 0)
						item.goodsPrice = item.amount
						item.goodsName ="提现金额"
						if(item.state === 0) {
							item.stateName = "支付中"
							item.color = "#f69045"
						}
						if(item.state === 1) {
							item.stateName = "成功"
							item.color = "#10B86B"
						}
						if(item.state === 2) {
							item.stateName = "取消"
							item.color ="#999999"
						}
						if(item.state === 3) {
							item.stateName = "失败"
							item.color ="#F11616"
						}
						if(item.state === 4) {
							item.stateName = "待审核"
							item.color ="#4599F6"
						}
					})
					 this.takeList = [...this.takeList,... this.videoList]
				}
				
		},
		// 跳详情页
		dealDetails() {
			uni.navigateTo({
				url: '/pages/my/deal/deal_details'
			})
		},
		// tba事件
		matabInput(e) {
			this.tabActive = e
			this.buyFrom.pageNum = 1
			this.takeList = []
			this.getBuy()
		},
		// 返回上一页
		backClick() {
			uni.switchTab({
			    url: '/pages/my/index'
			});
		}
	}
}
</script>

<style lang="scss" scoped>
	.tabs-box{
		height: 100rpx;
		border-bottom: 1px solid #F2F2F2;
		line-height: 100rpx;
		/deep/ .tab{
			height: 96rpx !important;
			line-height: 96rpx !important;
		}
		/deep/ .tab__line {
			height: 6rpx !important;
		}
		/deep/ .tab__item {
			line-height: 96rpx !important;
		}
		/deep/ .tab__item-title {
			margin: 0 35rpx !important;
		}
	}
	.main{
		padding: 27rpx;
		.deal-list{
			width: 100%;
			// height: 207rpx;
			background: #FFFFFF;
			box-shadow: 0px 8rpx 16rpx 0px rgba(124, 133, 236, 0.28);
			border-radius: 10rpx;
			font-size: 24rpx;
			color: #333333;
			margin-bottom: 20rpx;
			.item-title-time{
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid #F2F2F2;
				padding: 30rpx 0 23rpx 0;
				.title{
					font-size: 24rpx;
					margin-left: 24rpx;
				}
				.apply-time{
					color: #999999;
					margin-right: 27rpx;
				}
			}
			.take-put-money{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 27rpx 25rpx 32rpx 25rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				.money{
					color: #FF1111;
				}
			}
			.deal-time-status{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0rpx 25rpx 35rpx 25rpx;
				.item-time{
					color: #999999;
				}
				.status{
					font-family: PingFang SC;
					font-weight: 500;
					color: #4599F6;
				}
			}
		}
	}
</style>
